<?php
!defined('IN_FRAME') && die('404 Page');
class c_slide extends Controller {
    
    public $arrManagerData = null;
    public $mdlAcl = null;
    public $mdlSlide = null;
    
    public function __construct() {
        $mdlManager = Helper::loadModel('manager');
        if (!$mdlManager->logined()) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=login', '请先登录!');
        } else {
            $this->arrManagerData = $mdlManager->getSession();
            $this->mdlAcl = Helper::loadModel('acl');
            $this->mdlSlide = Helper::loadModel('slide');
        }
    }
    
    public function index() {
        if (!$this->mdlAcl->checkPermission($this->arrManagerData['group_name'], 'slide', 'view')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权查看!');
        }
        $arrAssign = array(
            'strMsg' => '',
            'strOk' => '',
            'strTitle' => '焦点图管理',
            'arrList' => $this->mdlSlide->select(array(), '*', 'ORDER BY `id` DESC')
        );
        if (count($_POST) > 0) {
            if (isset($_POST['add'])) {
                if (!$this->mdlAcl->checkPermission($this->arrManagerData['group_name'], 'slide', 'add')) {
                    Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=slide', '你无权添加!');
                }
                $arrTemp = array(
                    'path' => isset($_POST['new_path']) ? $_POST['new_path'] : '',
                    'title' => isset($_POST['new_title']) ? $_POST['new_title'] : '',
                    'link' => isset($_POST['new_link']) ? $_POST['new_link'] : '',
                    'list_order' => isset($_POST['new_list_order']) && is_numeric($_POST['new_list_order']) ? $_POST['new_list_order'] : 0,
                    'rec_status' => isset($_POST['new_rec_status']) ? 1 : 0
                );
                if (empty($arrTemp['path'])) {
                    $arrAssign['strMsg'] = '请上传图片!';
                } elseif ($this->mdlSlide->insert($arrTemp)) {
                    $arrAssign['strOk'] = '添加成功!';
                    $arrAssign['arrList'] = $this->mdlSlide->select(array(), '*', 'ORDER BY `id` DESC');
                } else {
                    $arrAssign['strMsg'] = '添加失败,请检查!';
                }
            } else {
                $arrPostKey = array_keys($_POST, '修改');
                if (count($arrPostKey) > 0) {
                    $int = null;
                    foreach ($arrPostKey as $strTemp) {
                        if (substr($strTemp, -7) == '_update') {
                            $int = str_replace('_update', '', $strTemp);
                            break;
                        }
                    }
                    if (!is_null($int)) {
                        if (!$this->mdlAcl->checkPermission($this->arrManagerData['group_name'], 'slide', 'update')) {
                            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=slide', '你无权修改!');
                        } else {
                            $arrTemp = array(
                                'title' => isset($_POST[$int . '_title']) ? $_POST[$int . '_title'] : '',
                                'link' => isset($_POST[$int . '_link']) ? $_POST[$int . '_link'] : '',
                                'list_order' => isset($_POST[$int . '_list_order']) ? $_POST[$int . '_list_order'] : $int,
                                'rec_status' => isset($_POST[$int . '_rec_status']) ? 1 : 0
                            );
                            if ($this->mdlSlide->update($arrTemp, $int)) {
                                $arrAssign['strOk'] = '修改成功!';
                                $arrAssign['arrList'] = $this->mdlSlide->select(array(), '*', 'ORDER BY `id` DESC');
                            } else {
                                $arrAssign['strMsg'] = '修改失败,请检查!';
                            }
                        }
                    }
                } else {
                    $arrPostKey = array_keys($_POST, '删除');
                    if (count($arrPostKey) > 0) {
                        $int = null;
                        foreach ($arrPostKey as $strTemp) {
                            if (substr($strTemp, -7) == '_delete') {
                                $int = str_replace('_delete', '', $strTemp);
                                break;
                            }
                        }
                        if (!is_null($int)) {
                            if (!$this->mdlAcl->checkPermission($this->arrManagerData['group_name'], 'slide', 'delete')) {
                                Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=slide', '你无权删除!');
                            } elseif ($this->mdlSlide->delete($int)) {
                                $arrAssign['strOk'] = '删除成功!';
                                $arrAssign['arrList'] = $this->mdlSlide->select(array(), '*', 'ORDER BY `id` DESC');
                            } else {
                                $arrAssign['strMsg'] = '删除失败,请检查!';
                            }
                        }
                    }
                }
            }
        }
        $this->_display('admin/slide_index.php', $arrAssign);
    }
    
}